#ifndef _MYI2C_h_
#define _MYI2C_h_

#include "main.h"
//#include "debug.h"

#define MYI2C_Tick  10//MS


#define Maxbytes    250
#define Wait_Ack_time  199

#define MYI2C_SCK_OD_Mode   MYI2C_GPIO_Config(pst,SCL_OUT)
#define MYI2C_SDA_OD_Mode   MYI2C_GPIO_Config(pst,SDA_OUT)
#define MYI2C_SDA_IN_Mode   MYI2C_GPIO_Config(pst,SDA_IN)

#define MYI2C_SCK_Clr()  MYI2C_GPIO_DATA(pst,SCL_L);MYI2C_Delay(pst->clr_tim_time)//799
#define MYI2C_SCK_Set()  MYI2C_GPIO_DATA(pst,SCL_H);MYI2C_Delay(pst->set_tim_time)//999

#define MYI2C_SDA_Clr()  MYI2C_GPIO_DATA(pst,SDA_L);MYI2C_Delay(pst->clr_tim_time)
#define MYI2C_SDA_Set()  MYI2C_GPIO_DATA(pst,SDA_H);MYI2C_Delay(pst->set_tim_time)

//#define Read_MYI2C_SDA	for(i=0;i<MaxPortNum;i++)MYI2C_GPIO_DATA(Read_IN,i)

#define SDA_OUT  0
#define SDA_IN   1
#define SCL_OUT  2

#define Read_IN  0
#define SCL_H   1
#define SCL_L   2
#define SDA_H   3
#define SDA_L   4

//#define SENSOR_PORT_TYPE  unsigned char
typedef struct
{
  GPIO_TypeDef *SDA_PORT;
	GPIO_TypeDef *SCL_PORT;
	uint16_t SDA_Pin;
	uint16_t SCL_Pin;	
	
	unsigned long Speed;
	unsigned char Adrr;

	unsigned int  wait_ack_time;	
	unsigned int  clr_tim_time;	
  unsigned int  set_tim_time;	

	
	unsigned int  timcnt;
	unsigned char ErrFlag;
	unsigned int  SetRTim;

	unsigned char reg[100];
	long  val;  //x100

}MYI2C_Struct;

extern  MYI2C_Struct sersor_stu[2];

/* Exported functions ------------------------------------------------------- */
char  MYI2C_Init(MYI2C_Struct *pst,GPIO_TypeDef *SDA_xPORT,uint16_t SDA_xPin,GPIO_TypeDef *SCL_xPORT,uint16_t SCL_xPin,unsigned char xAddr,unsigned int wait_ack_time,unsigned int clk_tim_time,unsigned int sda_tim_time);
void  MYI2C_Handle(MYI2C_Struct *pst);
/* Private functions ---------------------------------------------------------*/
void  MYI2C_Delay(unsigned long nTim);
void MYI2C_IIC_Start(MYI2C_Struct *pst);
void MYI2C_IIC_Stop(MYI2C_Struct *pst);
void MYI2C_IIC_Ack(MYI2C_Struct *pst,unsigned char ack);
unsigned char MYI2C_IIC_Wait_Ack(MYI2C_Struct *pst,unsigned int wait_time);
void MYI2C_Write_IIC_Byte(MYI2C_Struct *pst,unsigned char dat);
unsigned char MYI2C_Read_IIC_Byte(MYI2C_Struct *pst,unsigned char num);
//void  MYI2C_Reset(void);
unsigned char MYI2C_READ_FUNC (MYI2C_Struct *pst,unsigned char device_addr,unsigned char register_addr,unsigned char *pDat,unsigned char len);
unsigned char MYI2C_WRITE_FUNC(MYI2C_Struct *pst,unsigned char device_addr,unsigned char register_addr,unsigned char *pDat,unsigned char len);

void MYI2C_GPIO_Config(MYI2C_Struct *pst,unsigned char TYP);
unsigned char MYI2C_GPIO_DATA(MYI2C_Struct *pst,unsigned char TYP);
unsigned char  CheckCrc8(unsigned char *pDat,unsigned char Lenth);

#endif //_MYI2C_h_
